// my_task_os.c
#include "my_task_os.h"

static my_taskOS_Task task_list[MAX_TASKS];
static uint8_t task_count = 0;

void my_taskOS_Init(void) {
    task_count = 0;
    for(uint8_t i=0; i<MAX_TASKS; i++) {
        task_list[i].task_func = NULL;
    }
}

bool my_taskOS_AddTask(void (*task_func)(void), uint32_t interval_ms) {
    if(task_count >= MAX_TASKS) return false;
    
    for(uint8_t i=0; i<MAX_TASKS; i++) {
        if(task_list[i].task_func == NULL) {
            task_list[i].task_func = task_func;
            task_list[i].interval = interval_ms;
            task_list[i].next_run = HAL_GetTick() + interval_ms;
            task_count++;
            return true;
        }
    }
    return false;
}

void my_taskOS_RunScheduler(void) {
    uint32_t current_tick = HAL_GetTick();
    
    for(uint8_t i=0; i<MAX_TASKS; i++) {
        if(task_list[i].task_func != NULL && current_tick >= task_list[i].next_run) {
            task_list[i].task_func();
            task_list[i].next_run = current_tick + task_list[i].interval;
        }
    }
}